<script setup lang="ts">
import type { YearGroup } from '@/types/archived';

defineProps<{
  archives: YearGroup[];
  currentYear: number | null;
  expandedYears: number[];
}>();

const emit = defineEmits<{
  'scroll-to-year': [year: number];
  'scroll-to-month': [year: number, month: number];
  'toggle-year': [year: number];
}>();
</script>

<template>
  <div class="timeline-card">
    <div class="side-header">时间轴</div>
    <div class="year-list custom-scrollbar">
      <div
        v-for="year in archives"
        :key="year.year"
        class="year-group"
      >
        <div
          class="year-item"
          :class="{ active: year.year === currentYear }"
        >
          <div
            class="year-header"
            @click="emit('scroll-to-year', year.year)"
          >
            <span class="year">{{ year.year }}</span>
            <span class="count">{{ year.count }}</span>
          </div>
          <span
            class="expand-icon"
            @click.stop="emit('toggle-year', year.year)"
          >
            {{ expandedYears.includes(year.year) ? '−' : '+' }}
          </span>
        </div>

        <div
          v-if="expandedYears.includes(year.year)"
          class="month-list"
        >
          <div
            v-for="month in year.months"
            :key="month.month"
            class="month-item"
            @click="emit('scroll-to-month', year.year, month.month)"
          >
            <span class="month">{{ month.month }}月</span>
            <span class="count">{{ month.articles.length }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.timeline-card {
  background-color: #fff;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);

  .side-header {
    font-size: 16px;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid #eaecef;
  }

  .year-group {
    .year-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 8px 12px;
      border-radius: 6px;
      cursor: pointer;
      margin-bottom: 8px;
      transition: all 0.3s ease;

      .year-header {
        display: flex;
        align-items: center;
        gap: 8px;

        .year {
          font-size: 14px;
          font-weight: 500;
        }

        .count {
          font-size: 13px;
          color: #6b7280;
          background: rgba(96, 165, 250, 0.08);
          padding: 2px 6px;
          border-radius: 10px;
        }
      }

      .expand-icon {
        font-size: 16px;
        color: #6b7280;
        transition: all 0.3s ease;
      }

      &:hover,
      &.active {
        background: rgba(96, 165, 250, 0.08);
        color: #60a5fa;

        .expand-icon {
          color: #60a5fa;
        }
      }
    }

    .year-list {
      .year-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 8px 12px;
        border-radius: 6px;
        cursor: pointer;
        transition: all 0.3s ease;
        margin-bottom: 8px;

        &:hover,
        &.active {
          background: rgba(96, 165, 250, 0.08);
          color: #60a5fa;
        }

        .year {
          font-size: 14px;
          font-weight: 500;
        }

        .count {
          font-size: 13px;
          color: #6b7280;
          background: rgba(96, 165, 250, 0.08);
          padding: 2px 6px;
          border-radius: 10px;
        }
      }
    }

    .month-list {
      margin-top: 4px;
      padding-left: 12px;

      .month-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 6px 12px;
        border-radius: 6px;
        cursor: pointer;
        transition: all 0.3s ease;
        font-size: 13px;
        color: #4b5563;
        margin-bottom: 6px;

        .count {
          font-size: 12px;
          color: #6b7280;
          background: rgba(96, 165, 250, 0.05);
          padding: 1px 6px;
          border-radius: 8px;
        }

        &:hover {
          background: rgba(96, 165, 250, 0.05);
          color: #60a5fa;
          padding-left: 16px;
        }
      }
    }
  }
}
</style>
